package com.efast.cafe.portal.controller.company;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.enums.ResultStatus;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.portal.bean.base.BaseCompanyInfoBean;
import com.efast.cafe.portal.service.base.IBaseCompanyInfoService;

@Controller
@RequestMapping("/frame_center/company")
public class PortalCompanyInfoController extends AbstractController{
	
	@Autowired
	private IBaseCompanyInfoService baseCompanyInfoService;
	
	
	
	
	/**
	 * companyInfo 实现编辑操作
	 * @param request
	 * @param response
	 * @param portalCompanyInfo
	 * @param start
	 * @param length
	 * @return
	 * @throws JSONException
	 */
	@RequestMapping(value="/info/form")
	public  @ResponseBody ResultBean formPage(
			HttpServletRequest request, 
			HttpServletResponse response,
			ModelMap model){
		ResultBean result=new ResultBean(true,"");
		BaseCompanyInfoBean companyInfo= baseCompanyInfoService.queryByCode();
		result.setObject(companyInfo);
		return result;
	}
	
	/**
	 * 编辑companyInfo 信息操作
	 * @param request
	 * @param response
	 * @param portalCompanyInfo
	 * @return
	 */
	@RequestMapping(value = "/info/edit")
	public @ResponseBody ResultBean companyEdit(
			HttpServletRequest request, 
			HttpServletResponse response,
			@RequestBody BaseCompanyInfoBean portalCompanyInfo){
		ResultBean result=new ResultBean(true,"");
		try{
			baseCompanyInfoService.updateCompanyInfo(portalCompanyInfo,portalCompanyInfo.getAttachments());
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			result.setErrorCode(e.getMessage());
			
		}
		return result;
	}
	/**
	 * 企业下拉框
	 * @param 
	 * @return
	 */
	@RequestMapping(value="/list/companys")
	public @ResponseBody ResultBean queryComnanys(HttpSession session,
			HttpServletRequest request, HttpServletResponse response
			){
		ResultBean result = new ResultBean(true,null);
		try {
			result.setObject(baseCompanyInfoService.queryCompanys(session.getAttribute("request_locale").toString()));
		} catch (Exception e) {
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setErrorCode(e.getMessage());
			} else {
				result.setErrorCode(ResultStatus.SYS_ERROR.getCode());
			}
		}

		return result;
	}
}
